//
//  ZqlPictureSelectVC.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/27.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let coloumCount: CGFloat = 3

class ZqlPictureSelectVC: UICollectionViewController {
    
    init(){
        
        let layOut = UICollectionViewFlowLayout()
        //设置cell的位置
        let w = (ScreenWidth - (coloumCount + 1) * PictureCellMargin) / coloumCount
        layOut.itemSize = CGSize(width: w, height: w)
        layOut.minimumInteritemSpacing = PictureCellMargin
        layOut.minimumLineSpacing = PictureCellMargin
        //设置组间距
        layOut.sectionInset = UIEdgeInsets(top: PictureCellMargin, left: PictureCellMargin, bottom: 0, right: PictureCellMargin)
        super.init(collectionViewLayout: layOut)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(PictureSelectCell.self, forCellWithReuseIdentifier: reuseIdentifier)

            }
    }

//实现collectionView的数据源方法
extension ZqlPictureSelectVC{
    
      override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 9
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PictureSelectCell
        
        cell.backgroundColor = UIColor.lightGrayColor()
        //设置代理
        cell.psDelgate = self
        
        return cell
    }
}

//实现协议方法
extension ZqlPictureSelectVC:PictureSelectCellDelegate{
    
    func userWillChosePicture(){
        
    }
    
    func userWillDeletePicture(){
        
    }
}


//声明协议
protocol PictureSelectCellDelegate:NSObjectProtocol{
    //声明协议方法
    func userWillChosePicture()
    
    func userWillDeletePicture()
}



//自定义cell

private class PictureSelectCell:UICollectionViewCell{
    
    //声明代理属性 使用weak修饰,防止循环引用
    weak var psDelgate: PictureSelectCellDelegate?
    
    //实现按钮的点击事件监听方法
    @objc  private func addBtnDidClick(){
        psDelgate?.userWillChosePicture()
    }
    @objc private func deleteBtnDidClick(){
        psDelgate?.userWillDeletePicture()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置界面
    private func setUpUI(){
        
        contentView.addSubview(addBtn)
        contentView.addSubview(deleteBtn)
        //设置约束
        addBtn.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        deleteBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
        }
        
        //添加点击事件
        addBtn.addTarget(self, action: "addBtnDidClick", forControlEvents: .TouchUpInside)
        deleteBtn.addTarget(self, action: "deleteBtnDidClick", forControlEvents: .TouchUpInside)


    }
    //懒加载视图
    private lazy var addBtn:UIButton = UIButton(backImageName: "compose_pic_add", imageName: nil)
    private lazy var deleteBtn:UIButton = UIButton(backImageName: "compose_photo_close", imageName: nil)
}











